const path = require('path');
const jQuery = require('jquery');
const epointajax = require(path.join(__dirname, '../../../js/epointajax'));
const {
  allconfig
} = require(path.join(__dirname, '../../../js/config'));
const mustache = require("mustache");
const {
  ipcMain
} = require('electron').remote;
const {
  ipcRenderer
} = require('electron');

var paginvue;

jQuery(function ($) {

  const appinfotemplate = $("#appinfotemplate").html(),
    appdetailtemplate = $("#appdetailtemplate").html(),
    $tabcontent1 = $('#tab-content-1'),
    $tabcontent2 = $('#tab-content-2');

  //翻页vue对象


  //添加头上的tab切换功能
  $('#left').on('click', '.left-tab', function () {
    if (!$(this).hasClass('active')) {
      $('.left-tab').removeClass('active');
      $(this).addClass('active');
      //对应的内容显示
      $('.tab-content').addClass('hidden');
      $('#tab-content-' + $(this).data('id')).removeClass('hidden');
    }
  });


  initAppdetail();

  //获取我的应用
  function initAppdetail() {
    epointajax.post(allconfig.serverurl + '/rest/toolsrest/appdetail_list', {}).then((retinfo) => {

      $tabcontent1.html(mustache.render(appdetailtemplate, retinfo.custom));

      //添加点击事件
      $tabcontent1.on('click', '.common-frame', function () {
        //打开应用
        ipcRenderer.send('addTab', {
          title: $(this).data('appname'),
          url: $(this).data('appurl')
        });

      });
    }).catch(msg => {
      console.log(msg);
    });
  }

  //刷新我的收藏
  ipcMain.on('initAppdetail', (event, arg) => {
    initAppdetail();
  });

  //刷新应用中心
  ipcMain.on('initAllApp', (event, arg) => {
    initAllApp();
  });

  //刷新我的上架
  ipcMain.on('pageMyappinfo', (event, arg) => {
    pageMyappinfo();
  });


  initAllApp();

  function initAllApp() {
    //获取所有应用信息
    epointajax.post(allconfig.serverurl + '/rest/toolsrest/appinfo_list_all', {}).then((retinfo) => {
      //渲染列表
      $tabcontent2.html(mustache.render(appinfotemplate, retinfo.custom));

      //添加点击事件
      $tabcontent2.unbind('click').on('click', '.common-frame', function () {
        //打开应用详情页面
        ipcRenderer.send('opendialog', {
          title: '应用详情',
          url: '/render/appinfo/appinfodetail/appinfodetail.html?appguid=' + $(this).data('appguid')
        });
      });

    }).catch(msg => {
      console.log(msg);
    });

  }




  var pageindex = 0,
    pagesize = 5;
  pageMyappinfo();

  function pageMyappinfo() {
    return epointajax.post(allconfig.serverurl + '/rest/toolsrest/appinfo_list_my', {
      pageindex: pageindex,
      pagesize: pagesize
    }).then((retinfo) => {
      // new Promise().resove(retinfo);
      if (retinfo.status) {
        initPageVue(retinfo.custom.appinfolist, retinfo.custom.totalcount);
      }
    }).catch(msg => {
      console.log(msg);
    });
  }


  function initPageVue(appinfolist, total) {
    if (paginvue != null) {
      paginvue.table_list = appinfolist;
      paginvue.totalcount = total;
    } else {
      paginvue = new Vue({
        el: '#tab-content-3',
        data: {
          table_list: appinfolist,
          currentPage: pageindex,
          pagesize: pagesize,
          totalcount: total
        },
        methods: {
          handleCurrentChange: function (currentPage) {
            pageindex = currentPage - 1;
            pageMyappinfo();
          },
          publicformat: function (row, column, cellValue, index) {},
          handleEdit: function (index, row) {
            //打开应用编辑页面
            ipcRenderer.send('opendialog', {
              title: '应用编辑',
              url: '/render/appinfo/appinfoedit/appinfoedit.html?appguid=' + row.appguid
            });
          },
          handleDelete: function (index, row) {
            //确认后提示删除
            this.$confirm('此操作将删除应用, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              deleteappinfo(row.appguid).then((retinfo) => {
                console.log(retinfo)
                if (retinfo.status) {
                  this.$message({
                    type: 'success',
                    message: retinfo.text
                  });
                  //再刷新下列表
                  pageMyappinfo();
                }
              });

            }).catch(() => {

            });

          },
          addTab: function (index, row) {
            ipcRenderer.send('addTab', {
              title: row.appname,
              url: row.appurl
            });
          },
          appinfoadd: function () {
            //打开应用新增页面
            ipcRenderer.send('opendialog', {
              title: '应用上架',
              url: '/render/appinfo/appinfoadd/appinfoadd.html'
            });
          }
        }
      })
    }

  }


  //删除应用
  function deleteappinfo(appguid) {
    return new Promise((resolve, reject) => {
      epointajax.post(allconfig.serverurl + '/rest/toolsrest/appinfo_delete', {
        appguid: appguid
      }).then((retinfo) => {
        return resolve(retinfo);
      }).catch(msg => {
        console.log(msg);
        reject(msg);
      });
    })

  }


});
